package org.fujay.langchain4jserver.application.assembler;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.fujay.langchain4jserver.application.dto.ConversationDetailDTO;
import org.fujay.langchain4jserver.application.dto.ConversationSummaryDTO;
import org.fujay.langchain4jserver.application.dto.MessageDTO;
import org.fujay.langchain4jserver.domain.conversation.aggregate.ConversationAggregate;
import org.fujay.langchain4jserver.domain.conversation.entity.MessageEntity;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对话聚合 与 DTO 之间的转换器
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ConversationAssembler {

    public static MessageDTO toMessageDTO(final MessageEntity message) {
        if (message == null) return null;
        MessageDTO dto = new MessageDTO();
        dto.setId(message.getId().value());
        dto.setType(message.getType());
        dto.setContent(message.getContent());
        dto.setImageUrls(message.getImageUrls());
        dto.setTimestamp(message.getTimestamp() != null ? message.getTimestamp().toString() : null);
        dto.setMetadata(message.getMetadata());
        return dto;
    }

    public static List<MessageDTO> toMessageDTOList(final List<MessageEntity> messages) {
        if (CollectionUtils.isEmpty(messages)) return Collections.emptyList();
        return messages.stream().map(ConversationAssembler::toMessageDTO).collect(Collectors.toList());
    }

    public static ConversationSummaryDTO toSummaryDTO(final ConversationAggregate aggregate) {
        if (aggregate == null) return null;
        ConversationSummaryDTO dto = new ConversationSummaryDTO();
        dto.setId(aggregate.getId().value());
        dto.setUserId(aggregate.getUserId().value());
        dto.setAppId(aggregate.getAppId().value());

        // Extract summary info from messages if needed
        List<MessageEntity> messages = aggregate.getMessages(); // Assuming getter exists
        if (!CollectionUtils.isEmpty(messages)) {
            MessageEntity firstMessage = messages.get(0);
            MessageEntity lastMessage = messages.get(messages.size() - 1);
            dto.setLastMessageTimestamp(
                    lastMessage.getTimestamp() != null ? lastMessage.getTimestamp().toString() : null);
            // Simple preview: take first N chars
            String preview = firstMessage.getContent();
            if (preview != null && preview.length() > 50) {
                preview = preview.substring(0, 50) + "...";
            }
            dto.setFirstMessageContentPreview(preview);
        }
        return dto;
    }

    public static List<ConversationSummaryDTO> toSummaryDTOList(final List<ConversationAggregate> aggregates) {
        if (CollectionUtils.isEmpty(aggregates)) return Collections.emptyList();
        return aggregates.stream().map(ConversationAssembler::toSummaryDTO).collect(Collectors.toList());
    }

    public static ConversationDetailDTO toDetailDTO(final ConversationAggregate aggregate) {
        if (aggregate == null) return null;
        ConversationDetailDTO dto = new ConversationDetailDTO();
        dto.setId(aggregate.getId().value());
        dto.setUserId(aggregate.getUserId().value());
        dto.setAppId(aggregate.getAppId().value());
        dto.setCreatedAt(
                aggregate.getCreateTime() != null ? aggregate.getCreateTime().toString() : null);
        dto.setUpdatedAt(
                aggregate.getUpdateTime() != null ? aggregate.getUpdateTime().toString() : null);
        dto.setMessages(toMessageDTOList(aggregate.getMessages()));
        return dto;
    }


    // TODO: Methods to convert Request DTOs to domain parameters/objects if needed

}
